import requests
from lxml import etree

from Wallpaper.models import Wallpaper, Category


def get_proxy():
    return requests.get("http://demo.spiderpy.cn/get/").json()


def delete_proxy(proxy):
    requests.get("http://demo.spiderpy.cn/delete/?proxy={}".format(proxy))


def getHtml(url, headers):
    # ....
    retry_count = 5
    proxy = get_proxy().get("proxy")
    print(proxy)
    while retry_count > 0:
        try:
            html = requests.get(url, headers=headers, proxies={"http": "http://{}".format(proxy)})
            # 使用代理访问
            return html
        except Exception:
            retry_count -= 1
    # 删除代理池中代理
    delete_proxy(proxy)
    return None


class BiZhi4KWallpaperSpider:
    def __init__(self):
        self.classes = {
            "4K美食": "/meishi/", "4K风景": "/fengjing/", "4K美女": "/meinv/", "4K跑车": "/paoche/",
            "4K系统": "/xitong/", "4K游戏": "/youxi/", "4K影视": "/yingshi/", "4K动漫": "/donman/",
            "4K萌物": "/mengwu/"
        }
        self.keys = self.classes.keys()
        self.headers = {
            "Referer": "https://www.4kbizhi.com/",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36",
        }

    def main(self, class_name):
        page = 1
        url = f"https://www.4kbizhi.com{self.classes.get(class_name)}"
        req = getHtml(url, self.headers)
        while req.status_code == 200:
            print(page)
            try:
                category = Category.objects.get(name=class_name)
            except:
                category = Category(name=class_name)
                category.save()
            try:
                req.encoding = "gbk"
                tree = etree.HTML(req.text)
                imgs = tree.xpath("//ul[@class='sitem']/li/a/img")
            except AttributeError as e:
                print(f'共{page}页，已完成...')
                break
            for i in imgs:
                img = i.attrib
                src = "https://www.4kbizhi.com" + img.get("src")
                alt = img.get("alt")
                try:
                    wallpaper = Wallpaper(category=category, tag=alt.replace(" ", ","), url=src, title=alt)
                    wallpaper.save()
                except:
                    print("已存在")
            page += 1
            url = f"https://www.4kbizhi.com{self.classes.get(class_name)}index_{page}.html"
            req = getHtml(url, self.headers)

    def horizontal_wallpaper(self, class_name):
        page = 1
        url = f"https://www.4kbizhi.com{self.classes.get(class_name)}"
        req = getHtml(url, self.headers)
        while req.status_code == 200:
            print(page)
            try:
                category = Category.objects.get(name=class_name)
            except:
                category = Category(name=class_name)
                category.save()
            try:
                req.encoding = "gbk"
                tree = etree.HTML(req.text)
                imgs = tree.xpath("//ul[@class='item']/li/a/img")
            except AttributeError as e:
                print(f'共{page}页，已完成...')
                break
            for i in imgs:
                img = i.attrib
                src = "https://www.4kbizhi.com" + img.get("src")
                alt = img.get("alt")
                try:
                    wallpaper = Wallpaper(category=category, tag=alt.replace(" ", ","), url=src, title=alt,
                                          orientation=Wallpaper.HORIZONTAL)
                    wallpaper.save()
                except:
                    print("已存在")
            page += 1
            url = f"https://www.4kbizhi.com{self.classes.get(class_name)}index_{page}.html"
            req = getHtml(url, self.headers)


if __name__ == "__main__":
    BiZhi4KWallpaperSpider().horizontal_wallpaper("4K动漫")
